####################################################
# Replication files for:
#
# Toxic Speech and Limited Demand for Content
# Moderation on Social Media
#
####################################################

# R version 4.2.2 (2022-10-31)
# packages
library(tidyverse)
library(broom)
library(MetBrewer)
library(ggthemes)
library(stringr)
library(ggpubr)
library(dplyr)

# You might need to run the following code to install a specific version of marginal effects to replicate the Figures:
    #devtools::install_version("marginaleffects", version = "0.7.0", repos = "http://cran.us.r-project.org")

library(marginaleffects)

############################################################################
# STUDY I
############################################################################
######################################
# A: Data and settings
######################################
#setting seed
set.seed(84104)

# setting ggplot themes, palettes
palettes <- ggthemes_data[["tableau"]][["color-palettes"]][["regular"]]


# LGBTQ
load("../data/lgbtq.RData")
iit_lgbtq <- iit2 

rm(iit2)

# Billionaires
load("../data/billionaires.RData")
iit_billionaires <- iit2 
rm(iit2)


# Christians
load("../data/highly_religious.RData")
iit_religion <- iit2 
rm(iit2)

load("../data/partisans.RData")
iit_partisans <- iit2 
rm(iit2)

# Pooled dataset
allExp <- bind_rows(
  iit_lgbtq %>% 
     mutate(experiment = "LGBTQ"),
  iit_billionaires %>%
     mutate(experiment = "Billionaires"),
  iit_religion %>%
     mutate(experiment = "Christians")) %>%
  mutate(doSomething = ifelse(handle != "Leave it, do nothing", 1, 0),
         experiment = relevel(factor(experiment), ref="Christians"),
         pid = relevel(factor(pid), ref="Independent"),
         treatment = relevel(factor(treatment), ref="control")
  )



######################################
# B: Creating Table 1
######################################
### a: mean
table(allExp$treatment,allExp$rank1)
allExp$treatmentf <- factor(allExp$treatment,
       levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening")) #ordering the variable
library(reshape2)

rank1_pooled_data <- as.data.frame.matrix(table(allExp$treatmentf,allExp$rank1))
rank1_pooled_data$ngroup <- rank1_pooled_data$civil + rank1_pooled_data$uncivil + rank1_pooled_data$intolerant + rank1_pooled_data$threatening
rank1_pooled_data$civil_pct <- rank1_pooled_data$civil / rank1_pooled_data$ngroup
rank1_pooled_data$uncivil_pct <- rank1_pooled_data$uncivil / rank1_pooled_data$ngroup
rank1_pooled_data$intolerant_pct <- rank1_pooled_data$intolerant / rank1_pooled_data$ngroup
rank1_pooled_data$threatening_pct <- rank1_pooled_data$threatening / rank1_pooled_data$ngroup

#### b: confidence intervals
library(dplyr)
#creating binary variables, e.g., either having "civil" on rank1 or not
allExp$rank1_civil_binary <- ifelse(allExp$rank_civil=="1", 1, 0)
allExp$rank1_uncivil_binary <- ifelse(allExp$rank_uncivil=="1", 1, 0)
allExp$rank1_intolerant_binary <- ifelse(allExp$rank_intolerant=="1", 1, 0)
allExp$rank1_threatening_binary <- ifelse(allExp$rank_threatening=="1", 1, 0)
table(allExp$rank_civil, exclude=NULL)

rank1_pooled_data <- as.data.frame.matrix(table(allExp$treatmentf,allExp$rank1))
rank1_pooled_data$ngroup <- rank1_pooled_data$civil + rank1_pooled_data$uncivil + rank1_pooled_data$intolerant + rank1_pooled_data$threatening
rank1_pooled_data$civil_pct <- rank1_pooled_data$civil / rank1_pooled_data$ngroup
rank1_pooled_data$uncivil_pct <- rank1_pooled_data$uncivil / rank1_pooled_data$ngroup
rank1_pooled_data$intolerant_pct <- rank1_pooled_data$intolerant / rank1_pooled_data$ngroup
rank1_pooled_data$threatening_pct <- rank1_pooled_data$threatening / rank1_pooled_data$ngroup


ci_fun <- function(x, conf.level = 0.95) {
  n <- length(x)
  se <- sqrt(mean(x) * (1 - mean(x)) / n)
  z <- qnorm(conf.level + (1 - conf.level) / 2)
  c(mean(x) - z * se, mean(x) + z * se)
}

allExp$treatmentf=="non-group-related control"
allExp$rank1_civil_binary[which(allExp$treatmentf=="non-group-related control")]

#'civil'
#
library(purrr)
ci_civil_c1 <- ci_fun(allExp$rank1_civil_binary[which(allExp$treatmentf=="non-group-related control")])
ci_civil_c2 <-ci_fun(allExp$rank1_civil_binary[which(allExp$treatmentf=="control")])
ci_civil_t1 <-ci_fun(allExp$rank1_civil_binary[which(allExp$treatmentf=="uncivil")])
ci_civil_t2 <-ci_fun(allExp$rank1_civil_binary[which(allExp$treatmentf=="intolerant")])
ci_civil_t3 <-ci_fun(allExp$rank1_civil_binary[which(allExp$treatmentf=="threatening")])
ci_civil_df <- round(rbind(ci_civil_c1,ci_civil_c2,ci_civil_t1,ci_civil_t2,ci_civil_t3), digits=3)*100
colnames(ci_civil_df)<- c("ci_up_civil", "ci_low_civil")  
ci_civil_df <- data.frame(treatment=row.names(ci_civil_df),
                          ci_civil_df,row.names = c(1:nrow(ci_civil_df)))
ci_civil_df$cis_civil <- paste("[",ci_civil_df$ci_up_civil,", ", ci_civil_df$ci_low_civil, "]", sep="")
ci_civil_df            

#uncivil
ci_uncivil_c1 <- ci_fun(allExp$rank1_uncivil_binary[which(allExp$treatmentf=="non-group-related control")])
ci_uncivil_c2 <-ci_fun(allExp$rank1_uncivil_binary[which(allExp$treatmentf=="control")])
ci_uncivil_t1 <-ci_fun(allExp$rank1_uncivil_binary[which(allExp$treatmentf=="uncivil")])
ci_uncivil_t2 <-ci_fun(allExp$rank1_uncivil_binary[which(allExp$treatmentf=="intolerant")])
ci_uncivil_t3 <-ci_fun(allExp$rank1_uncivil_binary[which(allExp$treatmentf=="threatening")])
ci_uncivil_df <- round(rbind(ci_uncivil_c1,ci_uncivil_c2,ci_uncivil_t1,ci_uncivil_t2,ci_uncivil_t3), digits=3)*100
colnames(ci_uncivil_df)<- c("ci_up_uncivil", "ci_low_uncivil")  
ci_uncivil_df <- data.frame(treatment=row.names(ci_uncivil_df),
                            ci_uncivil_df,row.names = c(1:nrow(ci_uncivil_df)))
ci_uncivil_df$cis_uncivil <- paste("[",ci_uncivil_df$ci_up_uncivil,", ", ci_uncivil_df$ci_low_uncivil, "]", sep="")
ci_uncivil_df  

#intolerant
ci_intolerant_c1 <- ci_fun(allExp$rank1_intolerant_binary[which(allExp$treatmentf=="non-group-related control")])
ci_intolerant_c2 <-ci_fun(allExp$rank1_intolerant_binary[which(allExp$treatmentf=="control")])
ci_intolerant_t1 <-ci_fun(allExp$rank1_intolerant_binary[which(allExp$treatmentf=="uncivil")])
ci_intolerant_t2 <-ci_fun(allExp$rank1_intolerant_binary[which(allExp$treatmentf=="intolerant")])
ci_intolerant_t3 <-ci_fun(allExp$rank1_intolerant_binary[which(allExp$treatmentf=="threatening")])
ci_intolerant_df <- round(rbind(ci_intolerant_c1,ci_intolerant_c2,ci_intolerant_t1,ci_intolerant_t2,ci_intolerant_t3), digits=3)*100
colnames(ci_intolerant_df)<- c("ci_up_intolerant", "ci_low_intolerant")  
ci_intolerant_df <- data.frame(treatment=row.names(ci_intolerant_df),
                               ci_intolerant_df,row.names = c(1:nrow(ci_intolerant_df)))
ci_intolerant_df$cis_intolerant <- paste("[",ci_intolerant_df$ci_up_intolerant,", ", ci_intolerant_df$ci_low_intolerant, "]", sep="")
ci_intolerant_df  

#threatening
ci_threatening_c1 <- ci_fun(allExp$rank1_threatening_binary[which(allExp$treatmentf=="non-group-related control")])
ci_threatening_c2 <-ci_fun(allExp$rank1_threatening_binary[which(allExp$treatmentf=="control")])
ci_threatening_t1 <-ci_fun(allExp$rank1_threatening_binary[which(allExp$treatmentf=="uncivil")])
ci_threatening_t2 <-ci_fun(allExp$rank1_threatening_binary[which(allExp$treatmentf=="intolerant")])
ci_threatening_t3 <-ci_fun(allExp$rank1_threatening_binary[which(allExp$treatmentf=="threatening")])
ci_threatening_df <- round(rbind(ci_threatening_c1,ci_threatening_c2,ci_threatening_t1,ci_threatening_t2,ci_threatening_t3),digits=3)*100
colnames(ci_threatening_df)<- c("ci_up_threatening", "ci_low_threatening")  
ci_threatening_df <- data.frame(treatment=row.names(ci_threatening_df),
                                ci_threatening_df,row.names = c(1:nrow(ci_threatening_df)))
ci_threatening_df$cis_threatening <- paste("[",ci_threatening_df$ci_up_threatening,", ", ci_threatening_df$ci_low_threatening, "]", sep="")
ci_threatening_df  


rank1_table <- data.frame(treatment=c("C1: No group mentioned", "C2: Anti-Target", "T1: Uncivil", "T2: Intolerant", "T3: Threatening"),
                          civil_rank1=round(rank1_pooled_data[,6], digits=3)*100, 
                          civil_rank1_ci=ci_civil_df[,4], 
                          uncivil_rank1=round(rank1_pooled_data[,7], digits=3)*100, 
                          uncivil_rank1_ci=ci_uncivil_df[,4], 
                          intolerant_rank1=round(rank1_pooled_data[,8], digits=3)*100, 
                          intolerant_rank1_ci=ci_intolerant_df[,4], 
                          threatening_rank1=round(rank1_pooled_data[,9], digits=3)*100, 
                          threatening_rank1_ci=ci_threatening_df[,4])

rank1_table

library(xtable)
print(xtable(rank1_table, #digits = c(0,0,0,0,0,0,0,0,0,0)
),include.rownames=FALSE,type = "latex", file="../results/Table1.tex")


######################################
# C: Creating Figure 2
######################################
###################
# ESTIMATE MODELS
###################

mod1  <- glm(doSomething ~ treatment + experiment + pid, family="binomial", data=allExp)
mfx1 <- marginaleffects::marginaleffects(mod1)
mod1AMEs <- mfx1 %>% tidy() 

mod1AMEs <- mod1AMEs %>% 
  mutate(term = recode(term,
                       experiment = "Study",
                       pid = "Affiliation (Party)",
                       treatment = "Treatment")) %>%
  mutate(
    cont = case_when(
      contrast == "Republican - Independent"  ~ "Republican respondent",
      contrast == "Democrat - Independent" ~ "Democratic respondent",
      contrast == "Billionaires - Christians" ~ "Target: Billionaires",
      contrast == "LGBTQ - Christians" ~ "Target: LGBTQ",
      contrast == "non-group-related control - control" ~ "No group mentioned",
      contrast == "intolerant - control"~ "Intolerant post",
      contrast == "uncivil - control"  ~ "Uncivil post",
      contrast == "threatening - control"       ~ "Threatening post")
  ) %>%
  bind_rows(.,
            tribble(~ cont , ~ estimate, ~ term,
                    "Anti-target (baseline)",  0, "Treatment",
                    "Causal effects ↓",  NA, "Treatment",
                    "Target: Christians (baseline)",  0, "Study",
                    "Observables ↓",  NA, "Affiliation (Party)")
  ) %>%
  arrange(term,estimate) %>%
  mutate(
    ord = row_number(),
    colterm = c(4,3,3,rep(2,3),rep(1,6)))


(AME_all <- mod1AMEs %>%
    ggplot(aes(y=fct_reorder2(cont,term,-ord),x=estimate,
               color=factor(colterm),
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.35), size=.70) +
    scale_x_continuous(labels = scales::percent) +
    geom_vline(xintercept = 0, linetype=2) +
    scale_color_manual(values=c(`1`="black",`2`="darkgreen",`3`="blue",`4`="red")) +
    labs(x="Average Marginal Effect",
         y="",
         color="") +
    theme_bw() +
    theme(legend.position = "none",
          text = element_text(size=18)) +
    theme(axis.ticks.y = element_blank()) +
    theme(axis.text.y = element_text(face = c(rep('plain',2), 'bold',rep('plain',8),'bold')))
)

###########################
# STUDY BY STUDY AME PLOTS
###########################
M_exp1  <- glm(doSomething ~ ., family="binomial", data= allExp %>% filter(experiment=="LGBTQ") %>%
                 select(doSomething,
                        `Type of post` =  treatment)) # `Respondent PID` = pid))
M_exp2  <- glm(doSomething ~ ., family="binomial", data= allExp %>% filter(experiment=="Billionaires") %>%
                 select(doSomething,
                        `Type of post` =  treatment)) #`Respondent PID` = pid))
M_exp3  <- glm(doSomething ~ ., family="binomial", data= allExp %>% filter(experiment=="Christians") %>%
                 select(doSomething,
                        `Type of post` =  treatment)) # `Respondent PID` = pid))

(AME1 <- marginaleffects::marginaleffects(M_exp1) %>% tidy() %>%
    arrange(term,estimate) %>%
    mutate(
      cont = case_when(
        contrast == "Republican - Independent"  ~ "Republican respondent",
        contrast == "Democrat - Independent" ~ "Democratic respondent",
        contrast == "non-group-related control - control" ~ "No group mentioned",
        contrast == "intolerant - control"~ "Intolerant post",
        contrast == "uncivil - control"  ~ "Uncivil post",
        contrast == "threatening - control"       ~ "Threatening post")
    ) %>%
    bind_rows(.,
              tribble(~ cont , ~ estimate, ~ term,
                      "Anti-target (baseline)",  0, "Treatment")
    ) %>%
    ggplot(aes(y=fct_reorder(cont,estimate),x=estimate,
               #color=factor(colterm),
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.35), size=.75) +
    scale_x_continuous(labels = scales::percent, limits = c(-.3,.6)) +
    geom_vline(xintercept = 0, linetype=2) +
    scale_color_manual(values=c(`1`="black",`2`="darkgreen",`3`="blue",`4`="red")) +
    labs(
      subtitle = "Target: LGBTQ") +
    labs(x="Average Marginal Effect",y="",color="") +
    theme_bw() +
    theme(legend.position = "none",
          text = element_text(size=20))
)

(AME2 <- 
    marginaleffects::marginaleffects(M_exp2) %>% tidy() %>%
    arrange(term,estimate) %>%
    mutate(
      cont = case_when(
        contrast == "Republican - Independent"  ~ "Republican respondent",
        contrast == "Democrat - Independent" ~ "Democratic respondent",
        contrast == "non-group-related control - control" ~ "No group mentioned",
        contrast == "intolerant - control"~ "Intolerant post",
        contrast == "uncivil - control"  ~ "Uncivil post",
        contrast == "threatening - control"       ~ "Threatening post")
    ) %>%
    bind_rows(.,
              tribble(~ cont , ~ estimate, ~ term,
                      "Anti-target (baseline)",  0, "Treatment")
    ) %>%
    ggplot(aes(y=fct_reorder(cont,estimate),x=estimate,
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.35), size=.75) +
    scale_x_continuous(labels = scales::percent, limits = c(-.3,.6)) +
    geom_vline(xintercept = 0, linetype=2) +
    scale_color_manual(values=c(`1`="black",`2`="darkgreen",`3`="blue",`4`="red")) +
    labs(
      subtitle = "Target: Billionaires") +
    labs(x="Average Marginal Effect",y="",color="") +
    theme_bw() +
    theme(legend.position = "none",
          text = element_text(size=20))
)


(AME3 <- marginaleffects::marginaleffects(M_exp3) %>% tidy() %>%
    arrange(term,estimate) %>%
    mutate(
      cont = case_when(
        contrast == "Republican - Independent"  ~ "Republican respondent",
        contrast == "Democrat - Independent" ~ "Democratic respondent",
        contrast == "non-group-related control - control" ~ "No group mentioned",
        contrast == "intolerant - control"~ "Intolerant post",
        contrast == "uncivil - control"  ~ "Uncivil post",
        contrast == "threatening - control"       ~ "Threatening post")
    ) %>%
    bind_rows(.,
              tribble(~ cont , ~ estimate, ~ term,
                      "Anti-target (baseline)",  0, "Treatment")
    ) %>%
    ggplot(aes(y=fct_reorder(cont,estimate),x=estimate,
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.35), size=.75) +
    scale_x_continuous(labels = scales::percent, limits = c(-.3,.6)) +
    geom_vline(xintercept = 0, linetype=2) +
    scale_color_manual(values=c(`1`="black",`2`="darkgreen",`3`="blue",`4`="red")) +
    labs(
      subtitle = "Target: Christians") +
    labs(x="Average Marginal Effect",y="",color="") +
    theme_bw() +
    theme(legend.position = "none",
          text = element_text(size=20))
)


library(ggpubr)
figure2 <- ggpubr::ggarrange(AME_all + labs(subtitle = "Pooled data", x = "") + scale_x_continuous(labels = scales::percent, limits = c(-.25,.6)),
                  AME1 + labs(x = "") + theme(text = element_text(size=17)), 
                  AME3 + labs(x = "") + theme(text = element_text(size=17)),
                  AME2 + labs(x = "") + theme(text = element_text(size=17))) %>%
  annotate_figure(bottom = text_grob("Average marginal effect", size=18, vjust = -1.2))
figure2

#saving Figure 2 in the folder "results"
png("../results/Figure2.png", units="in", width=16, height=11, res=600)
figure2 # insert ggplot code
dev.off()

######################################
# D: Creating Figure 3
######################################

# settings for creating the figure
legend_label <- "How should social media companies \nhandle the post?"
textsize <- 21

# 1. creating subgraph: study LGBTQ
table(iit_lgbtq$treatment)
iit_lgbtq$treatment <- factor(iit_lgbtq$treatment, 
                              levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                              labels = c( "No group mentioned", "Anti-target",  "Uncivil post",  "Intolerant post", "Threatening post")) 

p_moderation_lgbtq <- iit_lgbtq %>% 
  group_by(treatment) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0))
p_moderation_lgbtq + ggtitle("Target: LGBTQ")


# 2. creating subgraph: study Billionaires
iit_billionaires$treatment <- factor(iit_billionaires$treatment, 
                                     levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                                     labels = c( "No group mentioned", "Anti-target",  "Uncivil post",  "Intolerant post", "Threatening post")) 

p_moderation_billionaires <- iit_billionaires %>% 
  group_by(treatment) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent\n",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0))
p_moderation_billionaires

# 3. creating subgraph: study Christians 
iit_religion$treatment <- factor(iit_religion$treatment, 
                                 levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                                 labels = c( "No group mentioned", "Anti-target",  "Uncivil post",  "Intolerant post", "Threatening post")) 

p_moderation_religion <-iit_religion %>% 
  group_by(treatment) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent\n",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0))
p_moderation_religion

# 4. creating subgraph pooled data (LGBTQ, Billionaires, Christians)
iit2_pooled <- allExp
iit2_pooled$treatment <- factor(iit2_pooled$treatment, 
                                     levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                                     labels = c( "No group mentioned", "Anti-target",  "Uncivil post",  "Intolerant post", "Threatening post")) 


library(stringr)
p_moderation_pooled <-iit2_pooled %>% 
  group_by(treatment) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0)) + 
  guides(fill=guide_legend(ncol=3,nrow=2,byrow=TRUE))
p_moderation_pooled  


# Creating final combined graph (Pooled, LGBTQ, Christians, Billionaires)

figure3 <- ggarrange(
  p_moderation_pooled, NULL, p_moderation_lgbtq, NULL, p_moderation_religion, NULL, p_moderation_billionaires, NULL, nrow=2, ncol=4, widths = c(1, 0.02, 1, 0.02),
  common.legend = TRUE, legend="bottom",
  labels = c("Pooled data", "", "Target: LGBTQ", "", "Target: Christians", "", "Target: Billionaires", ""), 
  font.label = list(size = 18))  %>% annotate_figure(top= text_grob("Preferred actions in response to distinct post types\n", face = "bold", size = 20))
figure3 

#saving Figure 3 in the folder "results" 
png("../results/Figure3.png", units="in", width=18, height=15, res=600)
figure3 # insert ggplot code
dev.off()

######################################
# E: Creating Figure 4
######################################


table(allExp$experiment, exclude=NULL)
(fig4A <-
    glm(doSomething ~ factor(treatment) * factor(pid) * factor(experiment), family="binomial", 
        data=allExp %>% mutate(pid = factor(pid,levels = c("Republican","Democrat","Independent")))) %>%
    marginaleffects(., by =c("treatment","experiment"), variables = c("pid")) %>%
    tidy() %>%
    mutate(cont = case_when(contrast == "mean(Democrat) - mean(Republican)" ~ "Pr(Take action|Democrat) -\nPr(Take action|Republican)")) %>%
    filter(!is.na(cont)) %>%
    filter(treatment %in% c("control","uncivil","intolerant","threatening")) %>%
    mutate(
      treatment = case_when(
        treatment ==  "control" ~ "Anti-target\nbut without hostility",
        treatment ==  "uncivil" ~ "Uncivil post" ,
        treatment == "intolerant" ~ "Intolerant post",
        treatment == "threatening" ~ "Threatening post")
    ) %>%
    ggplot(aes(y=cont,x=estimate,
               color=treatment,
               shape=experiment,
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.75), size=.5) +
    labs(x= "Partisan differences in demand for moderation
(higher values mean greater demand for moderation among Democrats)",
         caption = "",
         y="",
         color="Treatment",shape="Target") +
    theme_bw() +
    scale_color_tableau() +
    scale_x_continuous(labels = scales::percent) +
    geom_vline(xintercept = 0, linetype=2) +
    theme(text = element_text(size=15)) + facet_wrap(~treatment) )

(fig4B <-
    glm(doSomething ~ factor(treatment) * factor(pid) * factor(experiment), family="binomial", 
        data=allExp %>% mutate(pid = factor(pid,levels = c("Republican","Democrat","Independent")))) %>%
    marginaleffects(., by = c("pid","experiment"), variables = c("treatment")) %>%
    tidy() %>%
    filter(pid!="Independent") %>%
    mutate(var = case_when(
      contrast=="mean(intolerant) - mean(control)" ~ "Pr(Take action|intolerant) -\nPr(Take action|anti-target baseline)", 
      #contrast=="mean(non-group-related control) - mean(control)" ~ "Pr(Take action|movie) -\nPr(Take action|anti-target baseline)", 
      contrast=="mean(threatening) - mean(control)" ~ "Pr(Take action|threat) -\nPr(Take action|anti-target baseline)", 
      contrast=="mean(uncivil) - mean(control)" ~ "Pr(Take action|uncivil) -\nPr(Take action|anti-target baseline)"
    ),
    var2 = case_when(
      contrast=="mean(intolerant) - mean(control)" ~ "Effect of intolerant language", 
      contrast=="mean(threatening) - mean(control)" ~ "Effect of threatening language", 
      contrast=="mean(uncivil) - mean(control)" ~ "Effect of uncivil language"
    ), 
    ord = case_when(
      contrast=="mean(intolerant) - mean(control)" ~ 3, 
      contrast=="mean(non-group-related control) - mean(control)" ~ 1, 
      contrast=="mean(threatening) - mean(control)" ~ 4, 
      contrast=="mean(uncivil) - mean(control)" ~ 2
    )) %>%
    filter(!is.na(var)) %>%
    ggplot(aes(y=fct_reorder(var2,-ord),x=estimate,
               color=pid,shape=experiment,
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.45), size=.5) +
    labs(x= "Change in average probability of supporting content moderation\nrelative to an anti-target post without uncivil/tolerant/threatening language",
         y="",
         color="Respondent",shape="Target") +
    theme_bw() +
    scale_color_manual(values = c("red4","blue4")) +
    scale_x_continuous(labels = scales::percent) +
    geom_vline(xintercept = 0, linetype=2) +
    theme(text = element_text(size=15))
)



figure4 <- ggpubr::ggarrange(fig4A + labs(subtitle = "") + theme(plot.margin = margin(0, .1, 0, .55, "cm")),
                  fig4B + labs(subtitle = "") + theme(plot.margin = margin(0, 1.6, 0, 0, "cm")),
                  nrow =2,
                  hjust = -.1,
                  align = "h",
                  label.x = 0,
                  labels = c(
                    "Heterogeneity of partisan effects",
                    " Heterogenous effects"))
figure4

#saving Figure 4 in the folder "results" 
png("../results/Figure4.png", units="in", width=14, height=11, res=600)
figure4 # insert ggplot code
dev.off()

############################################################################
# STUDY II
############################################################################
######################
# A: Data and settings
######################
#loading republicans and democrats dataset 

load("../data/partisans.RData")
iit_partisans <- iit2 
rm(iit2)

iit_partisans$doSomething <- ifelse(iit_partisans$handle != "Leave it, do nothing", 1, 0)

iit_partisans$treatment2 <-factor(iit_partisans$treatment, 
                                 levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                                 labels = c( "No group \nmentioned", "Anti-target",  "Uncivil \npost",  "Intolerant \npost", "Threatening \npost")) 

iit_partisans$alltreatments <- factor(iit_partisans$alltreatments, 
                             levels = c("non-group-related control", "control (democrats)", "control (republicans)", "uncivil (democrats)", "uncivil (republicans)", "intolerant (democrats)","intolerant (republicans)", "threatening (democrats)", "threatening (republicans)"),
                             labels = c( "No group \nmentioned", "Anti-target\n(at Democrats)", "Anti-target\n(at Republicans)", "Uncivil post\n(at Democrats)", "Uncivil post\n(at Republicans)",  "Intolerant post\n(at Democrats)", "Intolerant post\n(at Republicans)", "Threatening post\n(at Democrats)", "Threatening post\n(Republicans)")) 
######################
# B: Creating Table 2
######################

rank1_partisans <- as.data.frame.matrix(table(iit_partisans$treatment,iit_partisans$rank1))
rank1_partisans$ngroup <- rank1_partisans$civil + rank1_partisans$uncivil + rank1_partisans$intolerant + rank1_partisans$threatening
rank1_partisans$civil_pct <- rank1_partisans$civil / rank1_partisans$ngroup
rank1_partisans$uncivil_pct <- rank1_partisans$uncivil / rank1_partisans$ngroup
rank1_partisans$intolerant_pct <- rank1_partisans$intolerant / rank1_partisans$ngroup
rank1_partisans$threatening_pct <- rank1_partisans$threatening / rank1_partisans$ngroup
rank1_partisans
rank1_partisans <- rank1_partisans[1:5,]

#creating binary variables, e.g., either having "civil" on rank1 or not
iit_partisans$rank1_civil_binary <- ifelse(iit_partisans$rank_civil=="1", 1, 0)
iit_partisans$rank1_uncivil_binary <- ifelse(iit_partisans$rank_uncivil=="1", 1, 0)
iit_partisans$rank1_intolerant_binary <- ifelse(iit_partisans$rank_intolerant=="1", 1, 0)
iit_partisans$rank1_threatening_binary <- ifelse(iit_partisans$rank_threatening=="1", 1, 0)



ci_fun <- function(x, conf.level = 0.95) {
  n <- length(x)
  se <- sqrt(mean(x) * (1 - mean(x)) / n)
  z <- qnorm(conf.level + (1 - conf.level) / 2)
  c(mean(x) - z * se, mean(x) + z * se)
}

iit_partisans$treatment=="non-group-related control"
iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="non-group-related control")]

#'civil'
ci_civil_c1 <- ci_fun(iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="non-group-related control")])
ci_civil_c2 <-ci_fun(iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="control")])
ci_civil_t1 <-ci_fun(iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="uncivil")])
ci_civil_t2 <-ci_fun(iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="intolerant")])
ci_civil_t3 <-ci_fun(iit_partisans$rank1_civil_binary[which(iit_partisans$treatment=="threatening")])
ci_civil_df <- round(rbind(ci_civil_c1,ci_civil_c2,ci_civil_t1,ci_civil_t2,ci_civil_t3), digits=3)*100
colnames(ci_civil_df)<- c("ci_up_civil", "ci_low_civil")  
ci_civil_df <- data.frame(treatment=row.names(ci_civil_df),
                          ci_civil_df,row.names = c(1:nrow(ci_civil_df)))
ci_civil_df$cis_civil <- paste("[",ci_civil_df$ci_up_civil,", ", ci_civil_df$ci_low_civil, "]", sep="")
ci_civil_df            

#uncivil
ci_uncivil_c1 <- ci_fun(iit_partisans$rank1_uncivil_binary[which(iit_partisans$treatment=="non-group-related control")])
ci_uncivil_c2 <-ci_fun(iit_partisans$rank1_uncivil_binary[which(iit_partisans$treatment=="control")])
ci_uncivil_t1 <-ci_fun(iit_partisans$rank1_uncivil_binary[which(iit_partisans$treatment=="uncivil")])
ci_uncivil_t2 <-ci_fun(iit_partisans$rank1_uncivil_binary[which(iit_partisans$treatment=="intolerant")])
ci_uncivil_t3 <-ci_fun(iit_partisans$rank1_uncivil_binary[which(iit_partisans$treatment=="threatening")])
ci_uncivil_df <- round(rbind(ci_uncivil_c1,ci_uncivil_c2,ci_uncivil_t1,ci_uncivil_t2,ci_uncivil_t3), digits=3)*100
colnames(ci_uncivil_df)<- c("ci_up_uncivil", "ci_low_uncivil")  
ci_uncivil_df <- data.frame(treatment=row.names(ci_uncivil_df),
                            ci_uncivil_df,row.names = c(1:nrow(ci_uncivil_df)))
ci_uncivil_df$cis_uncivil <- paste("[",ci_uncivil_df$ci_up_uncivil,", ", ci_uncivil_df$ci_low_uncivil, "]", sep="")
ci_uncivil_df  


#intolerant
ci_intolerant_c1 <- ci_fun(iit_partisans$rank1_intolerant_binary[which(iit_partisans$treatment=="non-group-related control")])
ci_intolerant_c2 <-ci_fun(iit_partisans$rank1_intolerant_binary[which(iit_partisans$treatment=="control")])
ci_intolerant_t1 <-ci_fun(iit_partisans$rank1_intolerant_binary[which(iit_partisans$treatment=="uncivil")])
ci_intolerant_t2 <-ci_fun(iit_partisans$rank1_intolerant_binary[which(iit_partisans$treatment=="intolerant")])
ci_intolerant_t3 <-ci_fun(iit_partisans$rank1_intolerant_binary[which(iit_partisans$treatment=="threatening")])
ci_intolerant_df <- round(rbind(ci_intolerant_c1,ci_intolerant_c2,ci_intolerant_t1,ci_intolerant_t2,ci_intolerant_t3), digits=3)*100
colnames(ci_intolerant_df)<- c("ci_up_intolerant", "ci_low_intolerant")  
ci_intolerant_df <- data.frame(treatment=row.names(ci_intolerant_df),
                               ci_intolerant_df,row.names = c(1:nrow(ci_intolerant_df)))
ci_intolerant_df$cis_intolerant <- paste("[",ci_intolerant_df$ci_up_intolerant,", ", ci_intolerant_df$ci_low_intolerant, "]", sep="")
ci_intolerant_df  

#threatening
ci_threatening_c1 <- ci_fun(iit_partisans$rank1_threatening_binary[which(iit_partisans$treatment=="non-group-related control")])
ci_threatening_c2 <-ci_fun(iit_partisans$rank1_threatening_binary[which(iit_partisans$treatment=="control")])
ci_threatening_t1 <-ci_fun(iit_partisans$rank1_threatening_binary[which(iit_partisans$treatment=="uncivil")])
ci_threatening_t2 <-ci_fun(iit_partisans$rank1_threatening_binary[which(iit_partisans$treatment=="intolerant")])
ci_threatening_t3 <-ci_fun(iit_partisans$rank1_threatening_binary[which(iit_partisans$treatment=="threatening")])
ci_threatening_df <- round(rbind(ci_threatening_c1,ci_threatening_c2,ci_threatening_t1,ci_threatening_t2,ci_threatening_t3),digits=3)*100
colnames(ci_threatening_df)<- c("ci_up_threatening", "ci_low_threatening")  
ci_threatening_df <- data.frame(treatment=row.names(ci_threatening_df),
                                ci_threatening_df,row.names = c(1:nrow(ci_threatening_df)))
ci_threatening_df$cis_threatening <- paste("[",ci_threatening_df$ci_up_threatening,", ", ci_threatening_df$ci_low_threatening, "]", sep="")
ci_threatening_df  


rank1_s2_table <- data.frame(treatment=c("C1: No group mentioned", "C2: Anti-Target", "T1: Uncivil", "T2: Intolerant", "T3: Threatening"),
                             civil_rank1=round(rank1_partisans[,6], digits=3)*100, 
                             civil_rank1_ci=ci_civil_df[,4], 
                             uncivil_rank1=round(rank1_partisans[,7], digits=3)*100, 
                             uncivil_rank1_ci=ci_uncivil_df[,4], 
                             intolerant_rank1=round(rank1_partisans[,8], digits=3)*100, 
                             intolerant_rank1_ci=ci_intolerant_df[,4], 
                             threatening_rank1=round(rank1_partisans[,9], digits=3)*100, 
                             threatening_rank1_ci=ci_threatening_df[,4])


rank1_s2_table
library(xtable)
print(xtable(rank1_s2_table, #digits = c(0,0,0,0,0,0,0,0,0,0)
),include.rownames=FALSE,type = "latex", file="../results/Table2.tex")

######################
# C: Creating Figure 5
######################
###################
# ESTIMATE MODELS
###################
iit_partisans$randomMovieGroup <- sample(1:2,nrow(iit_partisans),replace = T)

pidstudy <- bind_rows(
  iit_partisans %>% filter(partisan=="target: democrats") %>%
    mutate(experiment = "Democrats"),
  iit_partisans %>% filter(treatment=="non-group-related control" & randomMovieGroup==1) %>%
    mutate(experiment = "Democrats"),
  iit_partisans %>% filter(partisan=="target: republicans") %>%
    mutate(experiment = "Republicans"),
  iit_partisans %>% filter(treatment=="non-group-related control" & randomMovieGroup==2) %>%
    mutate(experiment = "Republicans"),
)  %>%
  mutate(doSomething = ifelse(handle != "Leave it, do nothing", 1, 0),
         experiment = relevel(factor(experiment), ref="Republicans"),
         pid = relevel(factor(pid), ref="Independent"),
         treatment = relevel(factor(treatment), ref="control")
  ) %>%
  mutate(pid7 = 
           case_when(
             pid7 == "Democrat" ~ 1,
             pid7 == "Independent" ~ 2,
             pid7 == "Leaning Democrat" ~ 3,
             pid7 == "Leaning Republican" ~ 4,
             pid7 == "Republican" ~ 5,
             pid7 == "Strong Democrat" ~ 6,
             pid7 == "Strong Republican" ~ 7
           )) 

HETPIDstudy <-
  glm(doSomething ~ factor(treatment) * factor(pid) * factor(experiment), family="binomial", 
      data = pidstudy %>% mutate(pid = factor(pid,levels = c("Republican","Democrat","Independent")))) %>%
  marginaleffects(., by =c("treatment","experiment"), variables = c("pid")) %>%
  tidy() %>%
  mutate(cont = case_when(contrast == "mean(Democrat) - mean(Republican)" ~ "Pr(Take action|Democrat) -\nPr(Take action|Republican)")) %>%
  filter(!is.na(cont)) %>%
  filter(treatment %in% c("control","uncivil","intolerant","threatening")) %>%
  mutate(
    treatment = case_when(
      treatment ==  "control" ~ "Anti-target\nbut without hostility",
      treatment ==  "uncivil" ~ "Uncivil post" ,
      treatment == "intolerant" ~ "Intolerant post",
      treatment == "threatening" ~ "Threatening post"),
    col = case_when(experiment == "Republicans" ~ "A",
                    experiment == "Democrats" ~ "B",
                    TRUE ~ "C")
  ) 



HETPID_B <-
  glm(doSomething ~ factor(treatment) * factor(pid) * factor(experiment), family="binomial", 
      data=pidstudy %>% mutate(pid = factor(pid,levels = c("Republican","Democrat","Independent")))) %>%
  marginaleffects(., by = c("pid","experiment"), variables = c("treatment")) %>%
  tidy() %>%
  filter(pid!="Independent") %>%
  mutate(var = case_when(
    contrast=="mean(intolerant) - mean(control)" ~ "Pr(Take action|intolerant) -\nPr(Take action|anti-target baseline)", 
    #contrast=="mean(non-group-related control) - mean(control)" ~ "Pr(Take action|movie) -\nPr(Take action|anti-target baseline)", 
    contrast=="mean(threatening) - mean(control)" ~ "Pr(Take action|threat) -\nPr(Take action|anti-target baseline)", 
    contrast=="mean(uncivil) - mean(control)" ~ "Pr(Take action|uncivil) -\nPr(Take action|anti-target baseline)"
  ),
  var2 = case_when(
    contrast=="mean(intolerant) - mean(control)" ~ "Effect of intolerant language", 
    contrast=="mean(threatening) - mean(control)" ~ "Effect of threatening language", 
    contrast=="mean(uncivil) - mean(control)" ~ "Effect of uncivil language"
  ), 
  ord = case_when(
    contrast=="mean(intolerant) - mean(control)" ~ 3, 
    contrast=="mean(non-group-related control) - mean(control)" ~ 1, 
    contrast=="mean(threatening) - mean(control)" ~ 4, 
    contrast=="mean(uncivil) - mean(control)" ~ 2
  ),
  target = case_when(
    experiment=="Democrats" ~ "A Democratic\ntarget",
    experiment=="Republicans" ~ "A Republican\ntarget"
  ),
  pid2 = case_when(
    pid=="Republican" ~ "Among Republicans",
    pid=="Democrat" ~"Among Democrats"
  ),
  lab = case_when(
    contrast=="mean(intolerant) - mean(control)" & experiment=="Democrats" ~ "A Democratic target",
    contrast=="mean(threatening) - mean(control)" & experiment=="Republicans" ~ "A Republican target"
  )) %>%
  filter(!is.na(var)) %>%
  group_by(pid) %>%
  mutate(mean_among_pid = mean(estimate)) %>%
  ungroup()


(HETPID_B %>%
    ggplot(aes(y=fct_reorder(var2,-ord),x=estimate,
               color=target,
               shape=experiment,
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.45), size=.5) +
    facet_grid(~pid2) +
    labs(x= "Change in average probability of supporting content moderation\nrelative to an anti-target post without uncivil/tolerant/threatening language",
         y="",
         subtitle="Heterogenous treatment effects by PID and target",
         color="",shape="Target") +
    theme_bw() +
    scale_color_manual(values = c("blue4","brown4")) +
    scale_shape_manual(values=c(17,16))+
    guides(colour = guide_legend(override.aes = list(shape = c(17,16)))) +
    guides(shape="none")+ #getting rid of the redundant shape legend
    scale_x_continuous(labels = scales::percent) +
    geom_vline(xintercept = 0, linetype=2) +
    theme(text = element_text(size=15)) -> panelA
)

table(pidstudy$treatment)
mod_pidstudy <- glm(doSomething ~ treatment + experiment + pid, family="binomial", data=pidstudy) %>%
  marginaleffects::marginaleffects() %>% 
  tidy() 

AMEs <- mod_pidstudy %>% 
  mutate(term = recode(term,
                       experiment = "Target",
                       pid = "Affiliation (Party)",
                       treatment = "Treatment")) %>%
  mutate(
    cont = case_when(
      contrast == "Republican - Independent"  ~ "Republican respondent",
      contrast == "Democrat - Independent" ~ "Democratic respondent",
      contrast == "Democrats - Republicans" ~ "Target: A Democrat",
      contrast == "non-group-related control - control" ~ "No group mentioned",
      contrast == "intolerant - control"~ "Intolerant language",
      contrast == "uncivil - control"  ~ "Uncivil language",
      contrast == "threatening - control" ~ "Threatening language")
  ) %>%
  bind_rows(.,
            tribble(~ cont , ~ estimate, ~ term,
                    "Anti-target (baseline)",  0, "Treatment",
                    "Causal effects ↓",  NA, "Treatment",
                    "Target: A Republican (baseline)",  0, "Target",
                    "Observables ↓",  NA, "Affiliation (Party)")
  ) %>%
  arrange(term,estimate) %>% 
  mutate(
    ord = row_number(),
    colterm = c(4,3,3,rep(2,2),rep(1,6)))


(AMEplot <- AMEs %>%
    ggplot(aes(y=fct_reorder2(cont,term,-ord),x=estimate,
               color=factor(colterm),
               xmin=conf.low,
               xmax=conf.high)) +
    geom_pointrange(na.rm=TRUE, position=position_dodge(width=0.35), size=.70) +
    scale_x_continuous(labels = scales::percent) +
    geom_vline(xintercept = 0, linetype=2) +
    scale_color_manual(values=c(`1`="grey15",`2`="darkolivegreen4",`3`="blue3",`4`="brown2")) +
    labs(x="Average Marginal Effect",
         y="",
         color="") +
    theme_bw() +
    theme(legend.position = "none",
          text = element_text(size=18)) +
    theme(axis.ticks.y = element_blank()) +
    theme(axis.text.y = element_text(face = c(rep('plain',2), 'bold',rep('plain',7),'bold')))
)


library(ggpubr)
figure5 <- ggpubr::ggarrange(AMEplot + 
                    labs(subtitle = "Study: Toxic speech against partisans") + 
                    scale_x_continuous(labels = scales::percent, limits = c(-.08,.6))+
                      theme(text = element_text(size=23))+
                      theme(axis.title.x = element_text(size=15)),
                  
                  panelA + labs(subtitle="Heterogenous treatment effects by PID and target",
                                x= "Change in average probability of supporting content moderation\nrelative to an anti-target post without uncivil/tolerant/threatening language") + 
                    theme(text = element_text(size=23)) +
                    theme(legend.position = "right") +
                   theme(axis.title.x = element_text(size=15)) +
                    guides(shape = "none"),
                  ncol = 1
)
figure5

#saving Figure 5 in the folder "results" 
png("../results/Figure5.png", units="in", width=14, height=11, res=600)
figure5 # insert ggplot code
dev.off()


######################
# D: Creating Figure 6
######################

legend_label <- "How should social media companies \nhandle the post?"
textsize <- 21
#y-labels without line break
iit_partisans$treatment3 <-factor(iit_partisans$treatment, 
                                   levels = c("non-group-related control", "control", "uncivil", "intolerant", "threatening"),
                                   labels = c( "No group mentioned", "Anti-target",  "Uncivil post",  "Intolerant post", "Threatening post")) 

iit2_democrats <- subset(iit_partisans, partisan=="target: democrats" | partisan=="non-group-related control")
iit2_republicans <- subset(iit_partisans, partisan=="target: republicans" | partisan=="non-group-related control")


# 1. creating graph democrats
p_moderation_democrats <- iit2_democrats %>% 
  group_by(treatment3) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment3),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0)) +
  guides(fill=guide_legend(ncol=3,nrow=2,byrow=TRUE))
p_moderation_democrats + ggtitle("Target: Democrats")



p_moderation_republicans <- iit2_republicans %>% 
  group_by(treatment3) %>%
  count(handle) %>%
  mutate(percent = n / sum(n) * 100) %>%
  ggplot(aes(y = fct_rev(treatment3),
             x = percent,
             fill = handle)) +
  geom_col(width = .45, alpha=.9) +
  labs(x = "Percent",
       y = "",
       title="",
       fill = legend_label) +
  theme_bw() +
  theme(text = element_text(size=textsize),
        legend.position = "right") +
  scale_fill_met_d(name = "Degas", direction = -1) +
  scale_x_continuous(expand = c(0, 0))
p_moderation_republicans + ggtitle("Target: Republicans")

# creating final Figure 6, a combined graph of the subgraphs of the studies on democrats target and republicans target

figure6 <- ggarrange(
  p_moderation_democrats, NULL, p_moderation_republicans, NULL, nrow=1, ncol=4, widths = c(1, 0.02, 1, 0.02),
  common.legend = TRUE, legend="bottom",
  labels = c("Target: Democrats\n", "", "Target: Republicans\n", ""), 
  font.label = list(size = 18))  %>% annotate_figure(top= text_grob("Preferred actions in response to distinct post types\n", face = "bold", size = 20))
figure6


png("../results/Figure6.png", units="in", width=19, height=12, res=600)
figure6 # insert ggplot code
dev.off()

